package com.sec.android.app.myfiles.module.local.foldertree;

import android.content.Context;
import android.net.Uri;

import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.util.SemFwWrapper;
import com.sec.android.app.myfiles.util.StorageMonitor;

import java.io.File;
import java.io.FilenameFilter;

public class FolderTreeRecord extends FileRecord {
    int mChildDirCnt = -1;
    boolean mOpened = false;

    public FolderTreeRecord(int id, String path, String name, long size, long date, int fileType, int isHidden, int itemCount,
                            int itemCountHidden) {
        super(StorageType.FolderTree, id, path, name, size, date, fileType, isHidden, itemCount, itemCountHidden);
    }

    public FolderTreeRecord(String fullPath) {
        mStorageType = StorageType.FolderTree;
        mPath = FileRecord.getPath(fullPath);
        mName = FileRecord.getName(fullPath);
    }

    @Override
    public String getDisplayName(Context context) {
        String ret = getName();
        if (isRoot()) {
            ret = StorageMonitor.getDisplayName(context, getFullPath());
        }
        return ret;
    }

    @Override
    public boolean isRoot() {
        return StorageMonitor.isStorageRoot(getFullPath());
    }

    @Override
    public boolean exists(Context context) {
        boolean exist;
        int storageType = StorageMonitor.getMatchedStorageType(getFullPath());
        exist = StorageMonitor.isStorageMounted(context, storageType);

        if (exist) {
            File srcFile = SemFwWrapper.file(getFullPath());
            exist = srcFile.exists();
        }
        return exist;
    }

    @Override
    protected int _getId() {
        // TODO Auto-generated method stub
        return -1;
    }

    @Override
    protected Uri _getUri(Context context) {
        return null;
    }

    public void setOpened(boolean bOpened) {
        mOpened = bOpened;
    }

    public boolean isOpened() {
        return mOpened;
    }

    @Override
    public int getItemCount(boolean showHidden) {
        int ret = getChildFileCount(getFullPath(), showHidden);
        if (showHidden) {
            mItemCountHidden = ret;
        } else {
            mItemCount = ret;
        }
        return ret;
    }

    public static int getChildFileCount(String path, final boolean includeHiddenFiles) {
        int nRet = 0;

        File file = SemFwWrapper.file(path);
        if (file.exists()) {
            String[] files = file.list(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String filename) {
                    return (includeHiddenFiles || !filename.startsWith("."));
                }
            });

            if (files != null)
                nRet = files.length;
        }
        return nRet;
    }

    public int hasChildDirCount(final boolean includeHiddenFiles) {

        if (mChildDirCnt < 0) {
            File file = SemFwWrapper.file(getFullPath());
            if (file.exists()) {
                File[] files = file.listFiles(new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String filename) {
                        return (includeHiddenFiles || !filename.startsWith("."));
                    }
                });

                if (files != null) {
                    for (File file1 : files) {
                        if (file1.isDirectory()) {
                            mChildDirCnt = 1;
                            break;
                        }
                    }
                }
            }

        }
        return mChildDirCnt;

    }

}
